package com.david.viewtest.view

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class BottomLineTabLayout (
    context: Context,
    attrs: AttributeSet,
) : RecyclerView(context, attrs) {

    var rect = Rect()
    var lineTop = 0f
    var paint = Paint()
    var lineLeft = 0f
    private var selectPosition =0
    private var lineWidth = 30f

    /**
     * tab点击之后要滚动的距离，用于滚动动画计算
     */
    private var scrollDistance = 0f

    /**
     * 上次底部线的left，用于滚动动画
     */
    private var startLineLeft = 0f


    /**
     * 动画对象
     */
    private val valueAnimator: ValueAnimator = ValueAnimator.ofFloat(0f, 1f)
    /**
     * 动画执行进度，0-1
     */
    private var animProgress = 0f

    var tabClickListener = OnClickListener {
        selectPosition = it.tag as Int
        val intArr = intArrayOf(0, 0)

        it.getLocationInWindow(intArr)

        scrollDistance = intArr[0] - lineLeft + (it.width/2 - lineWidth/2)

        startLineLeft = lineLeft

        valueAnimator.start()

        smoothScrollBy(intArr[0] - (width / 2 - it.width/2) , 0)
    }
    init {
        layoutManager = LinearLayoutManager(context, HORIZONTAL, false)
        adapter = TabAdapter()


        valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator) {
                animProgress = animation?.animatedValue as Float

                lineLeft = startLineLeft + scrollDistance * animProgress

                postInvalidate()
            }

        })

        valueAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}

            override fun onAnimationEnd(animation: Animator) {
            }

            override fun onAnimationCancel(animation: Animator) {}

            override fun onAnimationRepeat(animation: Animator) {}
        })

        post {
            lineLeft = getChildAt(0).width/2 - lineWidth/2
            postInvalidate()
        }
    }

    override fun onMeasure(widthSpec: Int, heightSpec: Int) {
        super.onMeasure(widthSpec, heightSpec)
        lineTop = measuredHeight - 10f
    }

    override fun onDraw(canvas: Canvas) {
        //绘制矩形
        //rect.left = lineLeft.toInt()
        //rect.top = lineTop.toInt()
        //rect.right = (lineLeft + lineWidth).toInt()
        //rect.bottom = height
        //canvas?.drawRect(rect, paint)
        //
        //Log.e("scroll", "${scrollX}")


        // 绘制平行四边形 创建一个路径对象
        //val path = Path()
        //path.moveTo(lineLeft + 10f, lineTop) // 移动到起始点
        //
        //path.lineTo(lineLeft + 50f, lineTop) // 绘制第一条边
        //
        //path.lineTo(lineLeft + 40f, height.toFloat()) // 绘制第二条边
        //
        //path.lineTo(lineLeft + 0f, height.toFloat()) // 绘制第三条边
        //
        //path.close() // 封闭路径
        //canvas?.drawPath(path, paint);
        super.onDraw(canvas)
    }


    inner class TabAdapter: Adapter<TabHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabHolder {
            var tab = TextView(context)
            //tab.layoutParams = LayoutParams(200, LayoutParams.MATCH_PARENT)
            tab.setPadding(50, 0, 50, 0)
            tab.setOnClickListener(tabClickListener)
            return TabHolder(tab)
        }

        override fun getItemCount(): Int {
            return 10
        }

        override fun onBindViewHolder(holder: TabHolder, position: Int) {
            holder.item.text = "tab$position"
            holder.item.tag = position
        }

    }

    inner class TabHolder(var item: TextView): ViewHolder(item){

    }


    //override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
    //    super.onScrollChanged(l, t, oldl, oldt)
    //    Log.e("scroll", "l: $l, oldl: $oldl")
    //    lineLeft += oldl
    //    startLineLeft += oldl
    //}
}